<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line('addBooking');?></h3>
<p class="required">Ghi chú : * Thông tin bắt buộc</p>
<style>
    .name_num_cls{
        width:20%;
    }
    .fone_num_cls{
        width:10%;
        text-transform: uppercase;
    }
    .uppercase{
        text-transform: uppercase;
    }
    input[type="radio"],button.btn.top{
        vertical-align: top;
    }
    .control{padding: 10px 10px;  display: inline-block}
    #buttonManual,#buttonAuto,#booking{display:none;}
    #apiVN1B{vertical-align: sub;color: red;}
    .required{color:red;}
</style>
<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" listcustomers-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
    <?php echo form_open('auth/booking/add',array('id' => 'ajaxform'));?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php 
                //mã KH
                echo form_label($this->lang->line('customerID'), 'customerID');
                echo form_input(array('id'=>'customerID_fake','value' => $customerID,'readonly' => 'true'));
                echo form_input(array('type' => 'hidden', 'name' => 'customerID', 'id' => 'customerID', 'value' => $customerID));
                echo form_error('customerID');
                
                //hãng hàng không
                echo form_label($this->lang->line('airline').' <span class="required">*</span>', 'airline');
                echo form_input(array('name' => 'airline', 'id' => 'airline', 'value' => '','placeholder' => $this->lang->line('airline'),'required'=>'required','class' => 'uppercase','maxlength'=>'2'));
                echo form_error('airline');
                echo ' <span style="color:red" id="simple-msg"></span>';
                
                //mã đặt chỗ
                echo form_label($this->lang->line('booking_place_code').' <span class="required">*</span>', 'booking_place_code');
                echo '';
                echo form_input(array('name' => 'booking_place_code', 'id' => 'booking_place_code', 'value' => '','placeholder' => $this->lang->line('booking_place_code'),'class' => 'uppercase','readonly' => 'true'));
                echo form_error('booking_place_code');
                
                //Loại booking
                echo form_label($this->lang->line('booking_types').' <span class="required">*</span>', 'booking_types');
                echo form_dropdown('booking_types', $this->booking_model->getListBookingTypes(), '1','id="booking_types"');//disabled
                echo form_error('booking_types');

                //mã booking
                echo $this->lang->line('booking').' <span class="required">*</span>';
                echo '<span class="control">';
                echo form_button(array('content' => 'Thêm booking bằng tay','class' => 'btn btn-success','id'=>'buttonManual')).' ';
                echo form_button(array('content' => 'Thêm booking tự động','class' => 'btn btn-danger','id'=>'buttonAuto'));
                echo ' <span id="button-load"></span>';
                echo '</span>';
                echo form_textarea(array('name' => 'controlBooking', 'id' => 'controlBooking', 'value' => '','rows'=>'5','readonly' => 'true','placeholder'=>$this->lang->line('pasteSource')));
                echo form_textarea(array('name' => 'booking', 'id' => 'booking', 'value' => '','rows'=>'5'));
                echo form_error('booking');
                
                echo '<div id="booking-msg" style="display:none">';
                echo '<div id="fone_addon1" class="fonediv">';
                
                echo form_label($this->lang->line('customersName'), 'customersName');
                echo form_textarea(array('name' => 'customersName', 'id' => 'customersName', 'value' => '','rows'=>'5','placeholder'=>$this->lang->line('customersName')));
                echo form_textarea(array('name' => 'customersName_fake', 'id' => 'customersName_fake', 'value' => '','rows'=>'5','placeholder'=>$this->lang->line('customersName')));
                echo form_error('customersName');
                
                //em bé
                echo form_label($this->lang->line('child'), 'child');
                echo form_input(array('name' => 'child', 'id' => 'child', 'value' => '','rows'=>'5','placeholder'=>$this->lang->line('child')));
                echo form_error('child');
                
                //NGAY BAY
                echo form_input(array('id' => 'datefly', 'name' => 'datefly','value' => '','class' => 'fone_num_cls','placeholder'=>$this->lang->line('datefly')));
                echo form_error('datefly');
                echo ' ';
                //số hiệu
                echo form_input(array('id' => 'numberfly','name' => 'numberfly', 'value' => '','class' => 'fone_num_cls','placeholder'=>$this->lang->line('numberfly')));
                echo form_error('numberfly');
                echo ' ';
                //Sb đi
                echo form_input(array('id' => 'gosb','name' => 'gosb', 'value' => '','class' => 'fone_num_cls','placeholder'=>$this->lang->line('gosb')));
                echo form_error('gosb');
                echo ' ';
                //Giờ đi
                echo form_input(array('id' => 'timefly','name' => 'timefly', 'value' => '','class' => 'fone_num_cls','placeholder'=>$this->lang->line('timefly')));
                echo form_error('timefly');
                echo ' ';
                //Sb đến
                echo form_input(array('id' => 'stopsb','name' => 'stopsb', 'value' => '','class' => 'fone_num_cls','placeholder'=>$this->lang->line('stopsb')));
                echo form_error('stopsb');
                echo ' ';
                //Giờ đến
                echo form_input(array('id' => 'timeat','name' => 'timeat', 'value' => '','class' => 'fone_num_cls','placeholder'=>$this->lang->line('timeat')));
                echo form_error('timeat');
                echo ' ';
                echo form_button(array('content' => $this->lang->line('add'),'class' => 'btn btn-success top'));
                echo form_input(array('type' => 'hidden', 'name' => 'booking-info[]', 'id' => 'booking-info-1', 'value' => ''));
                echo '</div>';
                echo '</div>';
                
                echo '<div id="booking-msg-1"></div>';
                //total_pax
                echo form_label($this->lang->line('total_pax'), 'total_pax');
                echo form_input(array('name' => 'total_pax', 'id' => 'total_pax', 'value' => '','readonly' => 'true','required'=>'required'));
                echo form_error('total_pax');
                //Giá net
                echo form_label($this->lang->line('net_fare').' <span class="required">*</span>', 'net_fare');
                echo form_input(array('name' => 'net_fare', 'id' => 'net_fare', 'value' => '','readonly' => 'true','required'=>'required'));
                echo form_error('net_fare');
                echo form_input(array('type' => 'hidden','name' => 'net_fare_fake', 'id' => 'net_fare_fake', 'value' => ''));
                //Giá bán
                echo form_label($this->lang->line('sale_fare').' <span class="required">*</span>', 'sale_fare');
                echo form_input(array('name' => 'sale_fare', 'id' => 'sale_fare', 'value' => '','readonly' => 'true','required'=>'required'));
                echo form_error('sale_fare');
                echo form_input(array('type' => 'hidden','name' => 'sale_fare_fake', 'id' => 'sale_fare_fake', 'value' => ''));
                //Lợi nhuận
                echo form_label($this->lang->line('profit'), 'profit');
                echo form_input(array('name' => 'profit', 'id' => 'profit', 'value' => '','readonly' => 'true'));
                echo form_error('profit');
                //Phương thức thanh toán
                echo form_label($this->lang->line('payment_method').' <span class="required">*</span>', 'payment_method');
                echo form_dropdown('payment_method', $this->booking_model->getListPaymentMethod(), '','id="payment_method" disabled="disabled"');
                echo form_error('payment_method');
                echo '<span id="payment_method_form"></span>';
                //booking tạm ứng
                echo '<span style="width:10%;float:left">'.$this->lang->line('advance').':</span>';
                $i = 0;
                while($i < 2){
                    echo form_radio(array("name"=>"advance","value"=>$i, 'checked'=>set_radio('advance', $i,($i == 0)?TRUE:FALSE )));
                    echo $this->lang->line(($i==0)?'no':'yes');
                    echo '&nbsp;&nbsp;&nbsp;&nbsp;';
                    $i++;
                }
                unset($i);
                
                //Ghi chú
                echo form_label($this->lang->line('note'), 'note');
                echo form_textarea(array('name' => 'note', 'id' => 'note', 'value' => '','readonly' => 'true','rows'=>'5'));
                echo form_error('note');
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary','disabled'=>'disabled')); ?>
                <a href="<?php echo site_url('auth/booking'); ?>" class="btn"><?php echo $this->lang->line('cancel')?></a>
        </div>
    <?php form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>
<?php $this->load->view('templates/backend/lib'); ?>
<!--<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">-->

<!--<script src="//code.jquery.com/jquery-1.10.2.js"></script>-->
<!--<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>-->
<script src="<?php echo base_url().'static/templates/backend/js/jquery.maskedinput.js';?>"></script>
<script src="<?php echo base_url().'static/templates/backend/js/autoNumeric.js';?>"></script>

<script>
$(function() {
  var $datepicker = $( "#datefly" );
  $datepicker.datepicker({ dateFormat: 'dd/mm/yy' }).val();
});
</script>

<script>
   $(document).ready(function() {
        $("#booking-msg").hide();
        
        $("#buttonManual").on('click',function() {
                $(this).attr('disabled','disabled');
                $("#buttonAuto").attr('disabled','disabled');
                $("#loading-image").show();
                setTimeout(function () {
                    $("#loading-image").hide();
                    $('#booking_place_code').removeAttr('readonly').focus();
                    $("#booking-msg").show();
                    $("#fone_addon2").remove();
                    $("#controlBooking").hide();
                    $("#customersName_fake").hide();

                    $("#net_fare").removeAttr('readonly');
                    $("#sale_fare").removeAttr('readonly');
                    $('#payment_method').removeAttr('disabled');
                    $("#note").removeAttr('readonly');
                    $('#submit').removeAttr('disabled'); 
                    $("#buttonAuto").hide();
                    $("#buttonManual").hide();
                },2000);
        });
        
        $("#buttonAuto").on('click',function() {
            var airline             = document.getElementById("airline").value;
            var controlBooking      = document.getElementById("controlBooking").value;
            
            if(airline === ""){
                alert("Vui lòng nhập hãng hàng không!");
                $("#airline").focus();
                return false;
            }
            if(controlBooking === ""){
                alert("Vui lòng paste Source vào ô bên dưới!");
                $("#controlBooking").focus();
                return false;
            }
            $("#loading-image").show();
            $(this).attr('disabled','disabled');
            $("#buttonManual").attr('disabled','disabled');
            setTimeout(function () {
                    $.ajax(
                        {
                            url : '/auth/getphone/getlists',
                            type: "POST",
                            data : { airline: airline,controlBooking: controlBooking},
                            cache: false,
                            beforeSend: function(){
                                
                            },
                            success:function(data) 
                            {
                                    $("#loading-image").hide();
                                    $("#buttonAuto").hide();
                                    $("#buttonManual").hide();
                                    $("#button-load").html("<img src='<?php echo base_url().'static/templates/backend/images/icon/ok.png';?>'/>");
                                    var data                = JSON.parse(data);
                                    var i,j,bookingval      ='',paxname = '',dot;
                                    var paxname_length      = data['paxname'].length;
                                    var flight_length       = data['flight'].length;
                                    
                                    if(paxname_length == 0 && flight_length == 0){
                                        
                                            $('#booking_place_code').removeAttr('readonly').focus();
                                            $("#booking-msg").show();
                                            $("#controlBooking").hide();
                                            $("#customersName_fake").hide();

                                            $("#net_fare").removeAttr('readonly');
                                            $("#sale_fare").removeAttr('readonly');
                                            $('#payment_method').removeAttr('disabled');
                                            $("#note").removeAttr('readonly');
                                            $('#submit').removeAttr('disabled'); 
                                            $("#buttonAuto").hide();
                                            $("#buttonManual").hide();
                                        
                                        return false;
                                    }
                                    
                                    for (i = 0; i < paxname_length; i++) {
                                        if(i == paxname_length-1){
                                            dot = "";
                                        } else {
                                            dot = "\n";
                                        }
                                        paxname += data['paxname'][i].trim() + dot;
                                    }
                                    
                                    for (j = 0; j < flight_length; j++) {
                                        if(j == (flight_length-1)){
                                            dot = "";
                                        } else {
                                            dot = "\n";
                                        }
                                        bookingval += data['flight'][j]['ngaybay']+'-'+data['flight'][j]['sohieu'].replace(/\s/g,'')+'-'+data['flight'][j]['sbdi'].replace('&nbsp;','-')+'-'+data['flight'][j]['sbden'].replace('&nbsp;','-')+dot;
                                    }

                                    document.getElementById("booking_place_code").value         = data['pnr'].trim();
                                    document.getElementById("total_pax").value                  = data['paxname'].length;
                                    document.getElementById("net_fare").value                   = FormatNumber(data['amount']);
                                    document.getElementById("net_fare_fake").value              = UnFormatNumber(data['amount']);
                                    document.getElementById("profit").value                     = FormatNumber(0);
                                    document.getElementById("booking").value                    = bookingval;
                                    document.getElementById("controlBooking").value             = bookingval;
                                    document.getElementById("customersName_fake").value         = paxname;
                                    document.getElementById("customersName").value              = paxname;
                                    
                                    //if(data['amount']== 0){
                                        $('#net_fare').removeAttr('readonly');
                                    //}
                                    $("#controlBooking").attr('disabled','disabled');
                                    $('#note').removeAttr('readonly');
                                    $('#payment_method').removeAttr('disabled');
                                    $('#sale_fare').removeAttr('readonly');
                                    $('#submit').removeAttr('disabled');
                                    $("#booking-msg").show();
                                    $('#customersName_fake').show();
                                    $('#customersName').hide();
                                    $('#datefly').hide();
                                    $('#numberfly').hide();
                                    $('#timefly').hide();
                                    $('#gosb').hide();
                                    $('#timeat').hide();
                                    $('#stopsb').hide();
                                    $('#booking-msg button.btn-success').hide();
                                    $("#net_fare").removeAttr('readonly');
                                     
                                    delete(data);delete(i);delete(j);delete(bookingval);delete(paxname);delete(paxname_length);delete(flight_length);
                            },
                            error: function() 
                            {
                               alert('Có lỗi xảy ra! Contact Admin!');return false;
                            }
                        });
                }, 2000);
        });
        $("#ajaxform").submit(function(e)
        {
            if($("#payment_method").val() == ""){
                alert("Vui lòng chọn phương thức thanh toán");
                return false;
            }
            $('button[type="submit"]').attr('disabled','disabled');
            $("#simple-msg-submit").html("<img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/>");
            var postData = $(this).serializeArray();
            var formURL = $(this).attr("action");
            setTimeout(function () {
                $.ajax(
                    {
                        url : formURL,
                        type: "POST",
                        data : postData,
                        cache: true,
                        beforeSend: function(){
                          $('button[type="submit"]').attr('disabled','disabled');
                        },
                        success:function(data) 
                        {                            
                            $('button[type="submit"]').attr('disabled','disabled');
                            if(data == 1){
                                $("#simple-msg-submit").html('<span style="color:red">Thêm Booking Hold thành công!</span>');
                                setTimeout("location.href = '/auth/bookingStatus/';",2000);
                            } else {
                                $("#simple-msg-submit").html('<span style="color:red">'+data+'</span>');    
                            }
                        },
                        error: function() 
                        {
                            $("#simple-msg-submit").html('Lỗi! Liên hệ a Phục');
                        }
                    });
            }, 222);
            e.preventDefault();	//STOP default action
        });
        
        $( "#payment_method" ).change(function() {
            updateURL = '/auth/booking/paymentMethod?id='+$(this).val()+'&cid=<?php echo $customerID;?>';
            $.ajax(
            {
                    url : updateURL,
                    success:function(data)
                    {
                       $("#payment_method_form").html(data);
                    },
                    error: function() 
                    {
                        $("#simple-msg").html('Vui lòng liên hệ Admin!');
                    }
            });
        }); 
    });

    $(function() {
        $.mask.definitions['a'] = "[A-Z]";
        $.mask.definitions['b'] = "[0-9]";
        $.mask.definitions['*'] = "[A-Z0-9]";
        
        $("#numberfly").mask("******",{placeholder:" ",autoclear:false});//SO HIEU
        $("#timefly").mask("99:99");//GIỜ BAY
        $("#gosb").mask("aaa");//SB ĐI
        $("#timeat").mask("99:99");//GIỜ ĐẾN
        $("#stopsb").mask("aaa");//SB ĐẾN

        $("input").blur(function() {
            $("#info").html("Unmasked value: " + $(this).mask());
        }).dblclick(function() {
            $(this).unmask();
        });
    });
    
    $("#timefly").blur(function() {
        var timesx=this.value;
        var times = timesx.split(":");
        if (timesx.indexOf('_',1) >0)
        {
            alert("Thời gian nhập không đúng");
            this.value='';
            $(this).focus();
        }else if (times[0] > 23 || times[1]>59)
        {
            alert("Thời gian nhập không đúng");
            this.value='';
            $(this).focus();
        }
    });
    
    $("#timeat").blur(function() {
        var timesx=this.value;
        var times = timesx.split(":");
        if (timesx.indexOf('_',1) >0)
        {
            alert("Thời gian nhập không đúng");
            this.value='';
            $(this).focus();
        }else if (times[0] > 23 || times[1]>59)
        {
            alert("Thời gian nhập không đúng");
            this.value='';
            $(this).focus();
        }
    });
    
    $('#net_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#net_fare").blur(function() {
        document.getElementById("net_fare_fake").value = UnFormatNumber($('#net_fare').val());
    });
    $('#sale_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#sale_fare").blur(function() {
        document.getElementById("sale_fare_fake").value = UnFormatNumber($('#sale_fare').val());
    });
    
    $('#profit').autoNumeric('init', {aSign:' VNĐ',mDec:0, pSign:'s'});
    
    $("#sale_fare").blur(function() {
        var tien = ($('#sale_fare').autoNumeric('get')-$('#net_fare').autoNumeric('get'));
        $('#profit').autoNumeric('set',tien);
    });
//    
    $("#datefly").blur(function() {
        document.getElementById("booking-info-1").value = document.getElementById("datefly").value 
        + '-' + document.getElementById("numberfly").value.trim()
        + '-' + document.getElementById("timefly").value
        + '-' + document.getElementById("gosb").value
        + '-' + document.getElementById("timeat").value
        + '-' + document.getElementById("stopsb").value;
    });
    
    $("#numberfly").blur(function() {
        if(document.getElementById("numberfly").value.trim()==""){
            alert("Vui lòng nhập thông tin vào! Nhớ bật Caplock");
            $(this).focus();
            return false;
        }
        document.getElementById("booking-info-1").value = document.getElementById("datefly").value 
        + '-' + document.getElementById("numberfly").value.trim()
        + '-' + document.getElementById("timefly").value
        + '-' + document.getElementById("gosb").value
        + '-' + document.getElementById("timeat").value
        + '-' + document.getElementById("stopsb").value;
    });
    
    $("#timefly").blur(function() {
        document.getElementById("booking-info-1").value = document.getElementById("datefly").value 
        + '-' + document.getElementById("numberfly").value.trim()
        + '-' + document.getElementById("timefly").value
        + '-' + document.getElementById("gosb").value
        + '-' + document.getElementById("timeat").value
        + '-' + document.getElementById("stopsb").value;
    });
    
    $("#gosb").blur(function() {
        if(document.getElementById("gosb").value.trim()==""){
            alert("Vui lòng nhập thông tin vào! Nhớ bật Caplock");
            $(this).focus();
            return false;
        }
        document.getElementById("booking-info-1").value = document.getElementById("datefly").value 
        + '-' + document.getElementById("numberfly").value.trim()
        + '-' + document.getElementById("timefly").value
        + '-' + document.getElementById("gosb").value
        + '-' + document.getElementById("timeat").value
        + '-' + document.getElementById("stopsb").value;
    });
    
    $("#timeat").blur(function() {
        document.getElementById("booking-info-1").value = document.getElementById("datefly").value 
        + '-' + document.getElementById("numberfly").value.trim()
        + '-' + document.getElementById("timefly").value
        + '-' + document.getElementById("gosb").value
        + '-' + document.getElementById("timeat").value
        + '-' + document.getElementById("stopsb").value;
    });
    
    $("#stopsb").blur(function() {
        if(document.getElementById("stopsb").value.trim()==""){
            alert("Vui lòng nhập thông tin vào! Nhớ bật Caplock");
            $(this).focus();
            return false;
        }
        document.getElementById("booking-info-1").value = document.getElementById("datefly").value 
        + '-' + document.getElementById("numberfly").value.trim()
        + '-' + document.getElementById("timefly").value
        + '-' + document.getElementById("gosb").value
        + '-' + document.getElementById("timeat").value
        + '-' + document.getElementById("stopsb").value;
    });
    
    function FormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
        }
    };
    ///,|VNĐ|\s/g
    function UnFormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/,|VNĐ|\s/g, "");
        }
    };
    //info booking

    $(document).ready(function() {
        var add_form = 2;
         $( "button.btn-success" ).click(function() {
            updateURL = '/auth/getphone/infoBooking?id='+add_form;
            $.ajax(
            {
                    url : updateURL,
                    success:function(data)
                    {
                       $("#booking-msg").append(data);
                       add_form = add_form + 1;
                    },
                    error: function() 
                    {

                    }
            });
        });
    });
    //with the new versions of jquery live() is deprecated instead you should use on() (note the new format):
    $("#customersName").on('blur',function() {
            var xtem = $.trim($('#customersName').val().toUpperCase());
            xtem = xtem.replace(/[^a-zA-Z-\r\n-\n\r-\r-\n-\s]/g,"");
            xtem = xtem.replace(/\r\n|\n\r|\r|\n/g,"-");

            var arr_name = xtem.split('-');
            $('#total_pax').val(arr_name.length);
            delete(xtem);delete(arr_name);
    });
    
    $("#airline").blur(function() {
            var airline = $("#airline").val().toUpperCase().replace(/\s/g, '');
            document.getElementById("airline").setAttribute("readonly", "true");
            if(airline == "VN" || airline == "1B"){
                $('#booking_place_code').removeAttr('readonly');
                //$('#booking_place_code').focus();
                //$('#airline').attr('disabled','disabled');
                //return api
            } else if (airline == "AK" || airline == "BL" || airline == "JT" || airline == "TR" || airline == "VJ"){
                $("#buttonManual").show();
                $("#buttonAuto").show();
                $('#controlBooking').removeAttr('readonly').focus();
                //$('#airline').attr('disabled','disabled');
            } else if (airline.length != 2){
                alert("Vui lòng nhập hãng hàng không gồm 2 chữ số!");$("#airline").focus();return false;
            } else {
//                $('#booking_place_code').removeAttr('readonly').focus();
                $('#booking_place_code').removeAttr('readonly');
                $("#booking-msg").show();
                $("#controlBooking").hide();
                $("#customersName_fake").hide();
                
                $("#net_fare").removeAttr('readonly');
                $("#sale_fare").removeAttr('readonly');
                $('#payment_method').removeAttr('disabled');
                $("#note").removeAttr('readonly');
                $('#submit').removeAttr('disabled');
                //$('#airline').attr('disabled','disabled');
            }
    });
    
    $("#booking_place_code").blur(function() {
        var sdata       = $("#booking_place_code").val().toUpperCase();
        var sairlines   = $("#airline").val().toUpperCase();
        
        if(/\s+/.test(sdata) || sdata==""){
            alert("Mã đặt chỗ chứa khoảng trống! Vui lòng xem lại");return false;
        }
        
        if(sairlines == "VN" || sairlines == "1B"){
            $("#loading-image").show();
            setTimeout(function () {
                    $.ajax(
                    {
                            type:'POST',
                            url : '/auth/booking/apiVN1B',
                            data : { sdata: sdata, sairlines:sairlines },
                            success: function(responseData) {
                                
                                $("#loading-image").hide();
                                if(responseData == ""){
                                                    ///$('#booking_place_code').removeAttr('readonly').focus();
                                                    $("#booking-msg").show();
                                                    $("#controlBooking").hide();
                                                    $("#customersName_fake").hide();

                                                    $("#net_fare").removeAttr('readonly');
                                                    $("#sale_fare").removeAttr('readonly');
                                                    $('#payment_method').removeAttr('disabled');
                                                    $("#note").removeAttr('readonly');
                                                    $('#submit').removeAttr('disabled');
                                } else {
                                            var data    = JSON.parse(responseData);
                                            var i,j,bookingval      ='',paxname = '',dot;
                                                var paxname_length      = data['paxname'].length;
                                                var flight_length       = data['flight'].length;

                                                for (i = 0; i < paxname_length; i++) {
                                                    if(i == paxname_length-1){
                                                        dot = "";
                                                    } else {
                                                        dot = "\n";
                                                    }
                                                    paxname += data['paxname'][i].trim() + dot;
                                                }

                                                for (j = 0; j < flight_length; j++) {
                                                    if(j == (flight_length-1)){
                                                        dot = "";
                                                    } else {
                                                        dot = "\n";
                                                    }
                                                    bookingval += data['flight'][j]['ngaybay']+'-'+data['flight'][j]['sohieu'].replace(/\s/g,'')+'-'+data['flight'][j]['sbdi'].replace('&nbsp;','-')+'-'+data['flight'][j]['sbden'].replace('&nbsp;','-')+dot;
                                                }

                                                document.getElementById("booking_place_code").value         = data['pnr'].trim();
                                                document.getElementById("total_pax").value                  = data['paxname'].length;
                                                document.getElementById("net_fare").value                   = FormatNumber(data['amount']);
                                                document.getElementById("net_fare_fake").value              = UnFormatNumber(data['amount']);
                                                document.getElementById("profit").value                     = FormatNumber(0);
                                                document.getElementById("booking").value                    = bookingval;
                                                document.getElementById("controlBooking").value             = bookingval;
                                                document.getElementById("customersName_fake").value         = paxname;
                                                document.getElementById("customersName").value              = paxname;

//                                                if(data['amount']== 0){
                                                    $('#net_fare').removeAttr('readonly');
//                                                }
                                                $("#booking_place_code").attr('readonly','true');
                                                $("#controlBooking").attr('disabled','disabled');
                                                $('#note').removeAttr('readonly');
                                                $('#payment_method').removeAttr('disabled');
                                                $('#sale_fare').removeAttr('readonly');
                                                $('#submit').removeAttr('disabled');
                                                $("#booking-msg").show();
                                                $('#customersName_fake').show();
                                                $('#customersName').hide();
                                                $('#datefly').hide();
                                                $('#numberfly').hide();
                                                $('#timefly').hide();
                                                $('#gosb').hide();
                                                $('#timeat').hide();
                                                $('#stopsb').hide();
                                                $('#booking-msg button.btn-success').hide();

                                                delete(data);delete(i);delete(j);delete(bookingval);delete(paxname);delete(paxname_length);delete(flight_length);
                                }
                            },
                            error: function (responseData) {
                                alert('POST failed.');
                            }
                    });

                },2000);
            }
    });
    
    document.getElementById("booking_types").onchange = function(){
        if(this.value == 2){
            document.getElementById("net_fare").value       = 0;
            document.getElementById("net_fare_fake").value  = 0;
        }
    };
    
    $("#customersName_fake").on('blur',function() {
                var xtem1 = $.trim($('#customersName_fake').val().toUpperCase());
                xtem1 = xtem1.replace(/[^a-zA-Z-\r\n-\n\r-\r-\n-\s]/g,"");
                xtem1 = xtem1.replace(/\r\n|\n\r|\r|\n/g,"-");

                var arr_name = xtem1.split('-');
                $('#total_pax').val(arr_name.length);
                delete(xtem1);delete(arr_name);
    });
    
    $("#child").keyup(function() {
        $(this).val($(this).val().replace(/([a-z])/,function(s){return s.toUpperCase()}));
    });
</script>